package com.jd.order.service.impl;

import com.jd.basic.service.impl.BaseServiceImpl;
import com.jd.basic.util.LoginContext;
import com.jd.basic.util.PageList;
import com.jd.order.domain.OrderPetAcquisition;
import com.jd.order.dto.OrderDto;
import com.jd.order.mapper.OrderPetAcquisitionMapper;
import com.jd.order.query.OrderPetAcquisitionQuery;
import com.jd.order.service.IOrderPetAcquisitionService;
import com.jd.org.domain.Employee;
import com.jd.org.domain.Shop;
import com.jd.org.mapper.EmployeeMapper;
import com.jd.org.mapper.ShopMapper;
import com.jd.pet.domain.Pet;
import com.jd.pet.domain.SearchMasterMsg;
import com.jd.pet.mapper.PetMapper;
import com.jd.pet.mapper.SearchMasterMsgMapper;
import com.jd.user.domain.Logininfo;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    @Transactional
    public PageList<OrderPetAcquisition> queryPageByRole(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        // 获取店铺id
        Long shop_id = employee.getShop_id();

        if (shop_id != null){
            query.setShopId(shop_id);

            Shop shop = shopMapper.loadById(shop_id);
            if (shop.getAdminId() != employee.getId()){
                query.setEmpId(employee.getId());
            }

        }

        return super.queryPage(query);
    }





    @Override
    @Transactional
    public void cancel(Long id) { //订单id
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        //1.shopId置空
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());

        //2.删除宠物信息 - 订单取消了，宠物就没有接回来。之前已经将宠物录入系统了
        petMapper.remove(order.getPetId());

        //3.改状态
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);
    }


    //确认订单
    @Override
    @Transactional
    public void confirm(OrderDto orderDto) {

        Long id = orderDto.getId();
        BigDecimal money = orderDto.getMoney();
        Integer paytype = orderDto.getPaytype();
        // 修改订单状态
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        order.setState(1);
        order.setPrice(money);
        order.setPaytype(paytype);
        orderPetAcquisitionMapper.update(order);

        // 修改寻主消息状态
        Long msgId = order.getSearchMasterMsgId();
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(msgId);
        msg.setState(2);
        searchMasterMsgMapper.update(msg);

        // 信息宠物金额
        Pet pet = petMapper.loadById(order.getPetId());
        pet.setCostprice(orderDto.getMoney());
        petMapper.update(pet);

    }


    // 取消订单
    @Override
    public void remove(Long id){

        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        // 将宠物放到寻主池
        Long msgId = order.getSearchMasterMsgId();
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(msgId);
        msg.setShopId(null);
        searchMasterMsgMapper.update(msg);

        // 删除宠物信息
        petMapper.remove(order.getPetId());


        super.remove(id);
    }


    // 重写添加
    @Override
    public void save(OrderPetAcquisition orderPetAcquisition) {
        super.save(orderPetAcquisition);

        try {
            // 查询订单的全部信息
            OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(orderPetAcquisition.getId());
            // 查询订单状态
            String value = String.valueOf(order.getId());
            // 转为消息对象
            Message<String> build = MessageBuilder.withPayload(value).build();
            // 设置消息延迟
            rocketMQTemplate.syncSend("petOrder",build,1000,3);

        }catch (Exception e) {
            e.printStackTrace();
        }


    }
}
